package com.bjqg.bjqg01.bjqg.activity.seekprice.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.activity.seekprice.bean.FlName;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by bjqg01 on 2018/8/29.
 */

public class Fladapter extends BaseAdapter {
    private Context mContext;
    private View.OnClickListener onClickListener;
    private OnItemClickListener mOnItemClickListener;
    private int selectedPos = -1;
    private String selectedText = "";
    private List<FlName> mData;

    public Fladapter(Context context, List<FlName> level) {
        this.mContext = context;
        this.mData = level;


        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedPos = (Integer) view.getTag();
                setSelectedPosition(selectedPos);
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, selectedPos);
                }
            }
        };
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 设置选中的position,但不通知刷新
     */
    public void setSelectedPositionNoNotify(int pos, List<FlName> level) {
        selectedPos = pos;
        mData = level;
        if (mData != null && pos < mData.size()) {
            selectedText = mData.get(pos).getName();
        }
    }

    /**
     * 设置选中的position,并通知刷新其它列表
     */
    public void setSelectedPosition(int pos) {
        if (mData != null && pos < mData.size()) {
            selectedPos = pos;
            selectedText = mData.get(pos).getName();
            notifyDataSetChanged();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_city, parent, false);
//        View view = View.inflate(mContext, R.layout.item_city, null);
        TextView continent_text = (TextView) view.findViewById(R.id.continent_text);
        AutoRelativeLayout allfather = (AutoRelativeLayout) view.findViewById(R.id.rll_father);
        AutoLinearLayout allleft = (AutoLinearLayout) view.findViewById(R.id.all_left);
        String placeName = mData.get(position).getName();

        continent_text.setText(placeName);

        if (selectedText != null && selectedText.equals(placeName)) {
            allfather.setBackgroundColor(Color.parseColor("#ffffff"));
            allleft.setVisibility(View.VISIBLE);
        } else {
            allfather.setBackgroundColor(Color.parseColor("#f3f5f9"));
            allleft.setVisibility(View.GONE);
        }
        view.setTag(position);
        view.setOnClickListener(onClickListener);
        return view;
    }

    /**
     * 重新定义菜单选项单击接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }
}
